<?php
class PdoClass{
    protected $pro;
    public function __construct()
    {
        $this->mem = new Memcache();
        $this->pro = new PDO("mysql:host=localhost;dbname=news",'root','root');
        $this ->mem->connect('localhost',112111);
    }
    /*
     * 添加
     * */

    public function add($name,$photo,$zuo)
    {
        $sql = "insert into dian set name='{$name}',photo='{$photo}',zuo='{$zuo}'";
        $res = $this->pro->exec($sql);
        return true;
    }
    /*
     * 展示
     * */
    public function show($page){
        $sql = "select * from dian";
        $res = $this->pro->query($sql);
        $size = 3;
        $count = $res->rowCount();
        $pagenum = ceil($count/$size);
        $offset = ($page-1)*$size;
        $sql1 = "select * from dian limit $offset,$size";
        $res1 = $this->pro->query($sql1);
        $data['pageinfo'] = $res1->fetchAll();
        $pagestr['prev'] = $page-1<1 ? 1:$page-1;
        $pagestr['next'] = $page+1>$pagenum ? $pagenum:$page+1;
        $pagestr['pagenum'] = $pagenum;
        $data['pagestr']=$pagestr;
        return $data;
  }
    /*
     * 删除
     * */
    public function delete($id){
        $sql = "delete from book where id=:id";
        $res = $this->pro->prepare($sql);
        $res->execute(['id'=>$id]);
        return true;
    }
    /*
     * 修改
     * */
    public function update($id,$name,$miao,$shu){
        $sql = "update book set name='{$name}',miao='{$miao}',shu='{$shu}' where id=:id";
        $res = $this->pro->prepare($sql);
        $res->execute(['id'=>$id]);
        return true;
    }
    /*
     * 查询一条数据
     * */
    public function find($id){
        $sql = "select * from book where id=:id";
        $res = $this->pro->prepare($sql);
        $res->execute(['id'=>$id]);
        return $res->fetch();
    }
    /*
     * 搜索
     * */
    public function sear($cha){
        $sql = "select * from book where name like '%$cha%'";
        $res = $this->pro->query($sql);
        $size = 3;
        $count = $res->rowCount();
        $pagenum = ceil($count/$size);
        $page = isset($_GET['page'])? $_GET['page']:'1';
        $offset = ($page-1)*$size;
        $sql1 = "select * from book where name like '%$cha%' limit $offset,$size";
        $res1 = $this->pro->query($sql1);
        $data['pageinfo'] = $res1->fetchAll();
        $pagestr['prev'] = $page-1<1 ? 1:$page-1;
        $pagestr['next'] = $page+1>$pagenum ? $pagenum:$page+1;
        $pagestr['pagenum'] = $pagenum;
        $data['pagestr']=$pagestr;
        return $data;
    }


}